
package com.swak.frame.dto.response;

import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;

import com.swak.frame.dto.base.RestApiCode;
import com.swak.frame.dto.base.SysRestCode;



/**
 * MapResponse 继承HashMap增加扩展字段
 * ClassName: MapResponse.java 
 * @author colley.ma
 * @date 2021年3月17日 下午3:37:12
 */
public class MapResponse extends HashMap<String, Object>{
	private static final long serialVersionUID = 557109484992748308L;
	public static final String SUCC_CODE = "200";
	public static MapResponse FAIL = new MapResponse(SysRestCode.SYS_ERR);
	
	public static MapResponse SUCC = new MapResponse();

	public MapResponse(int retCode, String retMessage) {
    	super.put("retCode", String.valueOf(retCode));
    	super.put("retMessage", retMessage);
    }
	
    public MapResponse(String retCode, String retMessage) {
    	super.put("retCode", retCode);
    	super.put("retMessage", retMessage);
    }

    public MapResponse(RestApiCode restCode) {
        this(restCode.getRetCode(), restCode.getRetMessage());
    }

    public MapResponse() {
    	this(SysRestCode.SUCC);
    }

    public MapResponse(Object result) {
    	super.put("result", result);
    	super.put("retCode", SysRestCode.SUCC.getRetCode());
    	super.put("retMessage", SysRestCode.SUCC.getRetMessage());
        
    }
    
    public void restCode(RestApiCode restCode) {
    	super.put("retCode", restCode.getRetCode());
    	super.put("retMessage", restCode.getRetMessage());
	}


    public String getRetCode() {
		return (String) super.get("retCode");
	}

	
	
	public String getRetMessage() {
		return (String) super.get("retMessage");
	}

	public boolean retSuccess() {
        if (SUCC_CODE.equals(StringUtils.trim(getRetCode()))) {
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }
	
	
	public static MapResponse error() {
		return new MapResponse(SysRestCode.SYS_ERR);
	}
	
	public static MapResponse error(String errMessage) {
		return new MapResponse(SysRestCode.SYS_ERR.getRetCode(),errMessage);
	}
	
	public static MapResponse success() {
		return new MapResponse(SysRestCode.SUCC);
	}
	
	@Override
	public MapResponse put(String key, Object value) {
        super.put(key, value);
        return this;
    }
	
	 public MapResponse retCode(String retCode) {
		 super.put("retCode", retCode);
         return this;
     }

     public MapResponse setting(RestApiCode restCode) {
    	 restCode(restCode);
         return this;
     }
     
     public MapResponse setting(SysRestCode restCode) {
    	 super.put("retCode", restCode.getRetCode());
     	super.put("retMessage", restCode.getRetMessage());
         return this;
     }

     public MapResponse retMessage(String retMessage) {
    	 super.put("retMessage", retMessage);
         return this;
     }

     public MapResponse setting(String retCode, String retMessage) {
    	 super.put("retCode", retCode);
      	 super.put("retMessage", retMessage);
         return this;
     }

     public MapResponse result(Object result) {
    	 super.put("result", result);
         return this;
     }
}
